﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Itfort;
using MySoft.Data;

namespace Users
{
	/// <summary>
	/// 验证服务实现。
	/// </summary>
	public class VerificationService : IVerificationService
	{
		private DbSession dbSession = new DbSession("Users");

		/// <summary>
		/// 创建Email验证信息。
		/// </summary>
		/// <param name="email">电子邮箱。</param>
		/// <param name="activeCode">验证代码。</param>
		/// <param name="expired">过期时间。</param>
		/// <param name="activeType">验证类型。</param>
		/// <returns></returns>
		public EmailVerification CreateEmailVerification(string email, string activeCode, DateTime expired, string activeType)
		{
			Utility.ThrowNullOrEmpty(email, "参数email不能为空");
			Utility.ThrowNullOrEmpty(activeCode, "参数activeCode不能为空");

			if (this.ExistsEmailAndActiveCode(email, activeCode))
			{
				throw new Exception("电子邮箱和验证码的组合已经存在");
			}

			_EmailVerification _emailVerification = new _EmailVerification();
			_emailVerification.Email = email;
			_emailVerification.ActiveCode = activeCode;
			_emailVerification.Expired = expired;
			_emailVerification.ActiveType = activeType;

			dbSession.Save(_emailVerification);
			return this.GetEmailVerification(email, activeCode);
		}

		/// <summary>
		/// 创建Email验证信息。
		/// </summary>
		/// <param name="email">电子邮箱。</param>
		/// <param name="activeType">验证类型。</param>
		/// <returns></returns>
		public EmailVerification CreateEmailVerification(string email, string activeType)
		{
			string activeCode = Guid.NewGuid().ToString("N");
			DateTime expired = DateTime.Now.AddDays(2);
			return this.CreateEmailVerification(email, activeCode, expired, activeType);
		}

		/// <summary>
		/// 获取邮箱验证信息。
		/// </summary>
		/// <param name="email">电子邮箱。</param>
		/// <param name="activeCode">验证代码。</param>
		/// <returns></returns>
		public EmailVerification GetEmailVerification(string email, string activeCode)
		{
			return dbSession.Single<_EmailVerification>(_EmailVerification._.Email == email && _EmailVerification._.ActiveCode == activeCode)
				.Convert2<EmailVerification>();
		}

		/// <summary>
		/// 检查邮箱验证信息是否有效。
		/// </summary>
		/// <param name="email">电子邮箱。</param>
		/// <param name="activeCode">验证代码。</param>
		/// <returns></returns>
		public bool CheckEmailVerificationIsValid(string email, string activeCode)
		{
			EmailVerification emailVerification = this.GetEmailVerification(email, activeCode);
			// 不存在
			if (emailVerification == null) return false;
			// 已使用
			if (emailVerification.IsUsed) return false;
			// 已过期
			if (emailVerification.Expired < DateTime.Now) return false;

			return true;
		}

		/// <summary>
		/// 使用邮箱验证信息。
		/// </summary>
		/// <param name="email">电子邮箱。</param>
		/// <param name="activeCode">验证代码。</param>
		/// <returns></returns>
		public void UseEmailVerification(string email, string activeCode)
		{
			Field[] fields = new Field[] { _EmailVerification._.UseTime, _EmailVerification._.IsUsed };
			object[] values = new object[] { DateTime.Now, true };
			WhereClip where = _EmailVerification._.Email == email && _EmailVerification._.ActiveCode == activeCode && _EmailVerification._.IsUsed == false;
			dbSession.Update<_EmailVerification>(fields, values, where);
		}

		/// <summary>
		/// 电子邮箱和验证代码的组合是否存在。
		/// </summary>
		/// <param name="email">电子邮箱。</param>
		/// <param name="activeCode">验证代码。</param>
		/// <returns></returns>
		public bool ExistsEmailAndActiveCode(string email, string activeCode)
		{
			WhereClip where = _EmailVerification._.Email == email && _EmailVerification._.ActiveCode == activeCode;
			return dbSession.Exists<_EmailVerification>(where);
		}
	}
}
